from peewee import *
from .settings import PEEWEE_DB_URI
import datetime


db = PostgresqlDatabase(
    PEEWEE_DB_URI['database'],  # Required by Peewee.
    user=PEEWEE_DB_URI['user'],  # Will be passed directly to psycopg2.
    password=PEEWEE_DB_URI['password'],  # Ditto.
    host=PEEWEE_DB_URI['host'])  # Ditto.)


class BaseModel(Model):
    class Meta:
        database = db


class Novel(BaseModel):
    id = IntegerField(primary_key=True)
    title = CharField(max_length=512)
    summary = TextField()
    category = CharField(max_length=512)
    tags = CharField(max_length=512)
    author = CharField(max_length=512)
    source_url = CharField(unique=2048)

    class Meta:
        table_name = 'hetushu_novel'


class Chapter(BaseModel):
    id = IntegerField(primary_key=True)
    title = CharField(max_length=512)
    parent = CharField(null=True, max_length=512)
    book_id = IntegerField()
    sort_no = IntegerField()
    source_url = CharField(unique=True, max_length=2048)
    content = TextField(null=True)

    class Meta:
        table_name = 'hetushu_chapter'


class Book(BaseModel):
    title = CharField(max_length=512)
    slug = CharField(max_length=512)
    sub_title = CharField(max_length=512)
    author = CharField(max_length=512)
    isbn = CharField(max_length=512)
    year = CharField(max_length=512)
    page_cnt = CharField(max_length=512)
    language = CharField(max_length=512)
    file_size = CharField(max_length=512)
    file_format = CharField(max_length=512)
    category = CharField(max_length=512)
    source_url = CharField(max_length=512)
    download_url = CharField(max_length=512)
    cover_url = CharField(max_length=512)
    summary = TextField(null=True)

    class Meta:
        table_name = 'allitebooks_book'

class Torrent(BaseModel):
    title = CharField(max_length=512)
    post_at = DateTimeField()
    file_size = CharField(max_length=512)
    file_cnt = IntegerField()
    visit_cnt = IntegerField()
    visit_hot = IntegerField()
    thunder_link = CharField(max_length=512)
    magnet_link = CharField(max_length=512)
    file_list = TextField()
    created_at = DateTimeField(default=datetime.datetime.now)


